package com.gxuwz.medical.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.List;

import com.gxuwz.medical.domain.area.Area;
import com.gxuwz.medical.domain.bx_record.BX_record;
import com.gxuwz.medical.domain.chronicdis.Chronicdis;
import com.gxuwz.medical.domain.family_member.Family_member;
import com.gxuwz.medical.domain.join_nh.Join_nh;
import com.gxuwz.medical.domain.medical.Medical;
import com.gxuwz.medical.domain.policy.Policy;
import com.gxuwz.medical.exception.DbException;
import com.gxuwz.medical.tool.MyTime;

public class BX_recordDao extends GenericDao<BX_record> {

	@Override
	protected BX_record handle(ResultSet rs) throws SQLException {

		String bx_record_code = rs.getString("bx_record_code");
		String user_id = rs.getString("user_id");
		String chronicdis_code = rs.getString("chronicdis_code");
		String id_card = rs.getString("id_card");
		String all_cost = rs.getString("all_cost");
		String bx_cost = rs.getString("bx_cost");
		Timestamp vis_time = rs.getTimestamp("vis_time");
		Timestamp bx_time = rs.getTimestamp("bx_time");
		String receipt = rs.getString("receipt");
		String vis_city = rs.getString("vis_city");
		String area_code = rs.getString("area_code");
		int bx_state = rs.getInt("bx_state");
		String year = rs.getString("year");

		Area area = new Area(area_code);
		Policy policy = new Policy(MyTime.getStringNowYear());
		Chronicdis chronicdis = new Chronicdis(chronicdis_code);
		Family_member family_member = new Family_member(id_card, id_card);
		Join_nh join_nh = new Join_nh();
		Medical medical = new Medical(vis_city);
		BX_record entity = new BX_record(bx_record_code, user_id, chronicdis_code, id_card, all_cost, bx_cost, vis_time,
				bx_time, receipt, vis_city, area_code, bx_state, year, area, policy, chronicdis, family_member, join_nh,
				medical);
		return entity;
	}

	public List<BX_record> findByKeyValue(String key, String value) throws DbException {
		try {
			String sqlSB = "";
			StringBuffer sb = new StringBuffer(1024);
			if (key != null && value != null) {
				sqlSB = sb.append(" and " + key + "='" + value + "'").toString();
			}
			String sql = "select SUM(bx_cost) from t_bx_record where 1=1 " + sqlSB + " and year = "
					+ MyTime.getStringNowYear();
			System.out.println(sql);
			Object[] params = {};

			return super.queryOjects(sql, params);

		} catch (SQLException e) {
			throw new DbException("慢性病证信息查询失败!", e);
		}
	}

	public List<BX_record> findByKV(String key, String value) throws DbException {
		try {
			String sqlSB = "";
			StringBuffer sb = new StringBuffer(1024);
			if (key != null && value != null) {
				sqlSB = sb.append(" and " + key + "='" + value + "'").toString();
			}
			String sql = "select * from t_bx_record where 1=1 " + sqlSB + "";

			Object[] params = {};

			return super.queryOjects(sql, params);

		} catch (SQLException e) {
			throw new DbException("慢性病证信息查询失败!", e);
		}
	}

	public List<BX_record> findByLike(String where) throws DbException {
		try {

			String sql = "select * from t_bx_record where 1=1 " + where + " ORDER BY chronicdis_code";
			System.out.println("sql==" + sql);
			Object[] params = {};

			return super.queryOjects(sql, params);

		} catch (SQLException e) {
			throw new DbException("按级别查找行政区域失败!", e);
		}
	}

}
